var ns, toast, app;
app = new Vue({
    el: '#app',
    data: {
        alllist: [],
        cxlist: [],
        list: [],
        xjlist: [],
        Allchecked: false,//是否全选
        showchoose: false,
        showsearch: false,//遮罩层
        chooseList: [],
        chooseposList: [],
        form: {
            keywords: '',
            CompanyID: -1,
        },
        positionlist: [],
        nopositionlist: [],
        CompanyModel: {},//当前选中的组织级别
        selComlist: [],//顶部-组织级别
        totalchooseList: [],//总的选择人员，传输的值
        SearchHeight: 125,

    },
    methods: {
        onSearchChange: function () {
            var that = this;
            that.form.keywords = that.form.keywords.trim();
            if (that.CompanyModel.DepartmentTypeID == 1) {
                if (that.form.keywords != '') {
                    let resultData = that.alllist;
                    that.list = resultData.filter((e) => {
                        return e.UserName.trim().indexOf(that.form.keywords) > -1;
                    });
                }
                else {
                    that.Allchecked = false;
                    that.list = [];
                }
            }
            else {
                if (that.form.keywords != '') {
                    let resultData = that.xjlist;
                    that.list = resultData.filter((e) => {
                        return e.UserName.trim().indexOf(that.form.keywords) > -1;
                    });
                }
                else {
                    that.Allchecked = false;
                    that.list = that.xjlist;
                }
            }
            that.onCheckList();
        },
        //常选人员
        get_cxdata: function () {
            var that = this;
            var options = {
                action: "pulloftenuserlist",
            };
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    if (data.status) {
                        that.cxlist = data.AuditOftenChooseUserList;
                        that.get_dataDepart();
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            }, {
                toast: true
            });
        },
        //所有人员
        get_data: function () {
            var that = this;
            var actionValue = "getuserlist";
            var options = {
                action: actionValue,
                KeyWords: that.form.keywords,
            };
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (data != null) {
                    if (data.status) {
                        that.list = data.data;
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            }, {
                toast: true
            });
        },
        //部门人员
        get_dataDepart: function () {
            var that = this;
            var options = { keywords: '', action: 'pulluserlist', DepartmentID: that.form.CompanyID };
            // console.log(JSON.stringify(options));
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                toast.hide(); 
                if (succeed) {
                    let resultDatas = data.UserList.DepartmentList;
                    let resultData = data.UserList.DepartmentList;
                    //所有人员
                    that.alllist = data.UserList.UserFilterList;
                    that.list = that.alllist;

                    //判断是否有公司  默认选中公司这一层
                    let fjlist = resultDatas.filter((val) => {
                        return val.ParentID==0&&val.ID!= 0;
                    });
                    if (fjlist.length > 0) {
                        // console.log(JSON.stringify(fjlist[0]));
                        that.doChooseNext(fjlist[0]);
                    }

                    //未分配数据
                    let wfplist = resultData.filter((val) => {
                        return val.ID == 0;
                    });
                    that.nopositionlist = wfplist;
                    for (let items of that.nopositionlist) {
                        items.IsSelected = false;
                        items.UserChildrenList = items.Children;
                    }

                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_dataOrg: function () {
            var that = this;
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            var options = { keywords: '', action: 'pulluserlist', DepartmentID: that.form.CompanyID };
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    let resultData = data.UserList.DepartmentList;
                    let UserData = data.UserList.UserFilterList;
                    if (resultData) {
                        // 部门
                        var resultList = resultData;
                        for (let items of resultList) {
                            items.UserChildrenList = [];
                            items.IsSelected = false;
                            if (items.DepartmentTypeID == 1) {
                                items.UserChildrenList = UserData;
                            }
                            if (items.DepartmentTypeID == 2) {
                                items.UserChildrenList = [];
                                if (items.Children.length > 0) {
                                    var options = { keywords: '', action: 'pulldepartmentuserlist', DepartmentID: items.ID };
                                    ns.post(options, function (succeed, data, err) {
                                        if (succeed) {
                                            items.UserChildrenList = data.UserFilterList;
                                        }
                                    });
                                    // items.UserChildrenList = that.get_departuser(items.ID);
                                }
                            }
                            if (items.DepartmentTypeID == 3 || items.DepartmentTypeID == 0) {
                                items.UserChildrenList = items.Children;
                            }
                        }

                        setTimeout(() => {
                            toast.hide();
                            that.positionlist = resultList;
                            // console.log(JSON.stringify(that.positionlist));
                            that.onCheckList();
                        }, 300);
                    };
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_departuser: function (departId) {
            var that = this;
            var options = { keywords: '', action: 'pulldepartmentuserlist', DepartmentID: departId };
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    //data.UserFilterList
                }
            });
        },
        //加载选中效果
        onCheckList: function () {
            var that = this;
            if (that.totalchooseList.length <= 0) {
                that.chooseposList = [];
                that.chooseList = [];
                that.totalchooseList = [];
            }
            if (that.chooseposList.length > 0) {
                if (that.positionlist.length > 0) {
                    for (let item of that.positionlist) {
                        let index = that.chooseposList.findIndex(p => p.ID === item.ID);
                        if (index >= 0) {
                            item.IsSelected = true;
                        }
                    }
                }
                if (that.nopositionlist.length > 0) {
                    for (let item of that.nopositionlist) {
                        let index = that.chooseposList.findIndex(p => p.ID === item.ID);
                        if (index >= 0) {
                            item.IsSelected = true;
                        }
                    }
                }
            }
            if (that.chooseList.length > 0) {
                if (that.cxlist.length > 0) {
                    for (let item of that.cxlist) {
                        let index = that.chooseList.findIndex(p => p.UserID === item.UserID);
                        if (index >= 0) {
                            item.IsSelected = true;
                        }
                    }
                }
                if (that.list.length > 0) {
                    for (let item of that.list) {
                        let index = that.chooseList.findIndex(p => p.UserID === item.UserID);
                        if (index >= 0) {
                            item.IsSelected = true;
                        }
                    }
                }
            }
            if (that.totalchooseList.length > 0) {
                for (let item of that.list) {
                    let index = that.totalchooseList.findIndex(p => p.UserID === item.UserID);
                    if (index >= 0) {
                        item.IsSelected = true;
                    }
                }
            }
        },
        clear_searchVal: function () {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        // do_search: function () {
        //     this.get_data();
        // },
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影    
        },
        //选择某个组织级别切换
        doComClick: function (item) {
            var that = this;
            
            setTimeout(function () {
                var topheight = document.getElementById('topSearch').offsetHeight + 15;
                that.SearchHeight = topheight;
            }, 300);

            that.Allchecked = false;
            // if (that.form.CompanyID != item.ID) {
                that.form.CompanyID = item.ID;
                that.CompanyModel = item;
                const index = that.selComlist.findIndex(p => p.ID === item.ID);
                var chulilist = [];
                for (var i = 0; i < that.selComlist.length; i++) {
                    if (i <= index) {
                        chulilist.push(that.selComlist[i]);
                    }
                }
                that.selComlist = chulilist;//重新赋值当前顶部组织列表
                that.get_dataOrg();
            // }
        },
        //选择下级
        doChooseNext: function (item) {
            var that = this;
            if (that.form.CompanyID != item.ID) {
                that.form.CompanyID = item.ID;
                var model = { ID: item.ID, text: item.DepartmentNmae, DepartmentTypeID: item.DepartmentTypeID };
                // console.log(JSON.stringify(model));
                that.CompanyModel = model;
                that.selComlist.push(that.CompanyModel);

                setTimeout(function () {
                    var topheight = document.getElementById('topSearch').offsetHeight + 15;
                    that.SearchHeight = topheight;
                }, 300);

                if (item.DepartmentTypeID == 3 || item.DepartmentTypeID == 0) {
                    that.positionlist = [];
                    that.list = item.UserChildrenList;
                    that.xjlist = item.UserChildrenList;
                }
                else {
                    if (item.DepartmentTypeID == 2) {
                        that.list = item.UserChildrenList;
                        that.xjlist = item.UserChildrenList;
                    }
                    that.get_dataOrg();
                }

                that.onCheckList();
            }
        },
        onAllCheck: function () {
            var that = this;
            that.Allchecked = !that.Allchecked;
            var position = [];
            var userlist = [];
            var cxuserlist = [];
            //数据收集
            if (that.form.keywords == '') {
                for (let item of that.positionlist) {
                    item.IsSelected = that.Allchecked;
                    position.push(item);
                }
                if (that.CompanyModel.DepartmentTypeID == 1) {
                    for (let item of that.nopositionlist) {
                        item.IsSelected = that.Allchecked;
                        position.push(item);
                    }

                    for (let item of that.cxlist) {
                        item.IsSelected = that.Allchecked;
                        cxuserlist.push(item);
                    }
                }
            }
            for (let item of that.list) {
                item.IsSelected = that.Allchecked;
                userlist.push(item);
            }
            //数据处理
            if (that.CompanyModel.DepartmentTypeID == 1 && that.form.keywords == '') {
                if (that.Allchecked) {
                    that.chooseposList = position;
                    that.chooseList = cxuserlist;
                    that.totalUserList();
                }
                else {
                    that.chooseposList = [];
                    that.chooseList = [];
                    that.totalchooseList = [];
                }
            }
            else {
                if (that.Allchecked) {    
                    if (position.length > 0&&that.form.keywords=='') {
                        that.chooseposList = that.chooseposList.concat(position);
                    }

                    that.chooseList = that.chooseList.concat(userlist);
                    let totaluserlist = that.chooseList;
                    var new_list = totaluserlist.filter(function (element, index, self) {
                        return self.findIndex(el => el.UserID == element.UserID) === index  //如果是根据name去重就江id改为name
                    })
                    that.chooseList = new_list;

                    const new_postlist = [...new Set(that.chooseposList)];// 选中部门去重
                    that.chooseposList = new_postlist;

                    that.totalUserList();
                }
                else {
                    if (position.length > 0&&that.form.keywords=='') {
                        for (let item of position) {
                            const index = that.chooseposList.findIndex(p => p.ID === item.ID);
                            if (index >= 0) {
                                that.chooseposList.splice(index, 1);
                            }

                        }
                    }
                    for (let item of userlist) {
                        const index = that.chooseList.findIndex(p => p.UserID===item.UserID);
                        if (index >= 0) {
                            that.chooseList.splice(index, 1);
                        }
                    }   
                    that.totalUserList();
                }
            }
        },
        doChoosUserPosition: function (item) {
            var that = this;
            item.IsSelected = !item.IsSelected;
            // console.log(JSON.stringify(item));
            if (item.IsSelected) {
                that.chooseposList.push(item);
            }
            else {
                const index = that.chooseposList.findIndex(p => p.ID === item.ID);
                that.chooseposList.splice(index, 1);
            }

            const new_list = [...new Set(that.chooseposList)];// 去重
            that.chooseposList = new_list;

            that.totalUserList();
        },
        doChoosUser: function (item) {
            var that = this;
            item.IsSelected = !item.IsSelected;
            if (item.IsSelected) {
                that.chooseList.push(item);
            }
            else {
                const index = that.chooseList.findIndex(p => p.UserID === item.UserID);
                that.chooseList.splice(index, 1)
            }

            // const new_list = [...new Set(that.chooseList)];// 去重
            // that.chooseList = new_list;

            let totaluserlist = that.chooseList;
            var new_list = totaluserlist.filter(function (element, index, self) {
                return self.findIndex(el => el.UserID == element.UserID) === index  //如果是根据name去重就江id改为name
            })
            that.chooseList = new_list;

            that.totalUserList();
        },
        tel_phone: function (item) {
            api.call({
                type: 'tel_prompt',
                number: item.phone
            });
        },
        totalUserList: function () {
            var that = this;
            that.totalchooseList = [];
            var totaluserlist = [];
            if (that.chooseList.length > 0) {
                totaluserlist = that.chooseList;
            }
            if (that.chooseposList.length > 0) {
                for (let item of that.chooseposList) {
                    if (item.UserChildrenList.length > 0) {
                        totaluserlist = totaluserlist.concat(item.UserChildrenList);
                    }
                }
            }

            var new_list = totaluserlist.filter(function (element, index, self) {
                return self.findIndex(el => el.UserID == element.UserID) === index  //如果是根据name去重就江id改为name
            })
            that.totalchooseList = new_list;
            // const new_list = [...new Set(that.totaluserlist)];// 去重
            // that.totalchooseList = new_list;
        },
        //提交
        do_save: function () {
            var that = this;
            if (that.totalchooseList.length == 0) {
                api.toast({
                    msg: '请选择人员',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            api.sendEvent({
                name: 'purchase_add',
                extra: {
                    data: that.totalchooseList,
                }
            });
            setTimeout(function () {
                api.closeWin();
            }, 500)
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();

    // ns.init_search();
    app.get_cxdata();//获取常用审核人员

};